"""
Utility module for unified response formatting and other helper functions
"""

from flask import jsonify, g
from datetime import datetime
import uuid

class ResponseUtil:
    """
    Utility class for creating unified API responses
    """
    
    @staticmethod
    def _get_trace_id():
        """
        Get trace ID from request context or generate a new one
        """
        trace_id = getattr(g, 'trace_id', None)
        if not trace_id:
            trace_id = str(uuid.uuid4())
        return trace_id
    
    @staticmethod
    def success(data=None, message="Success", trace_id=None):
        """
        Create a success response
        """
        response = {
            "success": True,
            "message": message,
            "data": data if data is not None else {},
            "timestamp": datetime.utcnow().isoformat() + 'Z',
            "traceId": trace_id if trace_id else ResponseUtil._get_trace_id()
        }
        return jsonify(response), 200
    
    @staticmethod
    def error(message="Error", data=None, status_code=400, trace_id=None):
        """
        Create an error response
        """
        response = {
            "success": False,
            "message": message,
            "data": data if data is not None else {},
            "timestamp": datetime.utcnow().isoformat() + 'Z',
            "traceId": trace_id if trace_id else ResponseUtil._get_trace_id()
        }
        return jsonify(response), status_code
    
    @staticmethod
    def created(data=None, message="Created successfully", trace_id=None):
        """
        Create a success response for created resources
        """
        response = {
            "success": True,
            "message": message,
            "data": data if data is not None else {},
            "timestamp": datetime.utcnow().isoformat() + 'Z',
            "traceId": trace_id if trace_id else ResponseUtil._get_trace_id()
        }
        return jsonify(response), 201
    
    @staticmethod
    def not_found(message="Resource not found", trace_id=None):
        """
        Create a not found response
        """
        response = {
            "success": False,
            "message": message,
            "data": {},
            "timestamp": datetime.utcnow().isoformat() + 'Z',
            "traceId": trace_id if trace_id else ResponseUtil._get_trace_id()
        }
        return jsonify(response), 404
    
    @staticmethod
    def unauthorized(message="Unauthorized", trace_id=None):
        """
        Create an unauthorized response
        """
        response = {
            "success": False,
            "message": message,
            "data": {},
            "timestamp": datetime.utcnow().isoformat() + 'Z',
            "traceId": trace_id if trace_id else ResponseUtil._get_trace_id()
        }
        return jsonify(response), 401
    
    @staticmethod
    def forbidden(message="Forbidden", trace_id=None):
        """
        Create a forbidden response
        """
        response = {
            "success": False,
            "message": message,
            "data": {},
            "timestamp": datetime.utcnow().isoformat() + 'Z',
            "traceId": trace_id if trace_id else ResponseUtil._get_trace_id()
        }
        return jsonify(response), 403